package com.mszlu.blog.controller;

import com.mszlu.blog.common.aop.LogAnnotation;
import com.mszlu.blog.common.cache.Cache;
import com.mszlu.blog.service.ArticleService;
import com.mszlu.blog.vo.Result;
import com.mszlu.blog.vo.params.ArticleParam;
import com.mszlu.blog.vo.params.PageParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author linKe
 *
 *
 * json数据进行交互
 */
@Api(value="文章模块",tags = "articlesApi")
@RestController
@RequestMapping("articles")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    /**
     * `@LogAnnotation  aop记录日志
     * @param pageParams 分页参数
     * @return  文章结果集
     * Cache 增加缓存处理
     */
    @ApiOperation("首页获取文章列表")
    @PostMapping
    @LogAnnotation(module="文章",operator="获取文章列表")
    @Cache(expire = 5 * 60 * 1000,name = "listArticle")
    public Result listArticle(@RequestBody PageParams pageParams){
        // 测试全局异常处理 int i = 10/0;
        return articleService.listArticle(pageParams);
    }



    @ApiOperation("首页获取热门文章信息,用了redis缓存")
    @PostMapping("hot")
    @Cache(expire = 5 * 60 * 1000,name = "hot_article")
    public Result hotArticle(){
        int limit = 5;
        return articleService.hotArticle(limit);
    }

    @ApiOperation("首页获取最新文章信息,用了redis缓存")
    @PostMapping("new")
    @Cache(expire = 5 * 60 * 1000,name = "news_article")
    public Result newArticles(){
        int limit = 5;
        return articleService.newArticles(limit);
    }

    @ApiOperation("文章归档，根据年月分部显示")
    @PostMapping("listArchives")
    public Result listArchives(){
        return articleService.listArchives();
    }


    @ApiOperation("文章详情显示")
    @ApiImplicitParam(value = "路径参数id")
    @PostMapping("view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }




    @ApiOperation("文章发布接收")
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);
    }


}
